package br.com.hmm.businesslogic;

import java.util.List;

import br.com.hmm.dao.ContaSocioDAO;
import br.com.hmm.dao.SocioDAO;
import br.com.hmm.entidade.Conta;
import br.com.hmm.entidade.ProLabore;
import br.com.hmm.entidade.Socio;
import br.com.hmm.mb.ManterSocioBean;

public class SocioService {
	
	
	private SocioService(){
		
	}
	
	public static boolean inserir (Socio socio, Conta conta) throws Exception{
		try{
			SocioDAO sDao = new SocioDAO();
			if(sDao.inserir(socio)){
				if(associarSocioConta(conta.getId(), sDao.getUltimaIdSocio()))
				return true;
			}
		}catch (Exception e) {
			throw new Exception(e.getLocalizedMessage());
		}
		return false;
	}
	
	
	public static boolean update(Socio socio) throws Exception{
		try{
			SocioDAO sDao = new SocioDAO();
			if(sDao.atualizar(socio)){
				return true;
			}
		}catch(Exception e){
			throw new Exception(e.getLocalizedMessage());
		}
		return false;
	}
	
	
	public static boolean delete(Socio socio) throws Exception{
		try{
			SocioDAO sDao = new SocioDAO();
			if(sDao.delete(socio)){
				return true;
			}
		}catch (Exception e) {
			throw new Exception(e.getLocalizedMessage());
		}
		return false;
	}
	
	
	private static boolean associarSocioConta(int idConta, int ultimoId) throws Exception{
		ProLabore pro;
		ContaSocioDAO csDao;
		try{
			pro = new ProLabore();
			pro.getConta().setId(idConta);
			pro.getSocio().setId(ultimoId);
			csDao = new ContaSocioDAO();
			if(csDao.inserir(pro)){
				return true;
			}
		}catch (Exception e) {
			throw new Exception(e.getLocalizedMessage());
		}
		return false;
	}
	
	public static List<Socio> listarSociosConta(int idConta) throws Exception{
		try{
			SocioDAO sDao = new SocioDAO();
			List<Socio> ls = sDao.listSocioPorConta(idConta);
			if(!ls.isEmpty()){
				return ls;
			}
		}catch (Exception e) {
			throw new Exception(e.getLocalizedMessage());
		}
		return null;
	}
	


	

}
